package io.github.shenbinglife.springboot2.layui.admin.controller;

import io.github.shenbinglife.springboot2.layui.admin.entity.User;
import io.github.shenbinglife.springboot2.layui.admin.model.UserDetail;
import io.github.shenbinglife.springboot2.layui.admin.service.impl.UserService;
import io.github.shenbinglife.springboot2.layui.common.model.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import java.beans.PropertyEditorSupport;

@RestController
@RequestMapping("admin/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("page")
    public Result<Page<User>> list(@RequestParam(required = false) String keyword,
            @RequestParam(required = false) String name,
                                   @RequestParam(required = false) String orgName,
                                   @RequestParam(required = false) String roleName,
                                   @RequestParam(required = false) String orgId,
                                   @RequestParam(required = false) String roleId,
                                   @RequestParam(required = false) String notOrgId,
                                   @RequestParam(required = false) String notRoleId,
                                   @RequestParam(required = false) String mobilePhone,
                                   @RequestParam(defaultValue = "1") Integer page,
                                   @RequestParam(defaultValue = "20") Integer limit) {
        Page<User> pageData = userService.page(keyword, name,orgName, roleName, orgId, roleId, notOrgId, notRoleId,
                mobilePhone, page - 1, limit);
        return Result.success(pageData);
    }

    @GetMapping("{userId}")
    public Result<User> getById(@PathVariable("userId") Long userId) {
        return userService.getOptionalById(userId)
                .map(Result::success)
                .orElse(Result.failure("用户不存在"));
    }

    @PutMapping
    public Result<UserDetail> create(@RequestBody UserDetail user) {
        UserDetail save = userService.createUser(user);
        return Result.success(user);
    }

    @PostMapping("{userId}")
    public Result<Void> update(@PathVariable("userId") Long userId, @RequestBody UserDetail user) {
        assert userId.equals(user.getId());
        userService.updateUser(user);
        return Result.success();
    }

    @PostMapping("{id}/state")
    public Result<Void> updateState(@PathVariable("id") Long id, @RequestParam User.UserState state) {
        userService.updateState(id, state);
        return Result.success();
    }

    @DeleteMapping("{userId}")
    public Result<Void> delete(@PathVariable("userId") Long[] userIds) {
        userService.deleteUsers(userIds);
        return Result.success();
    }

    @PostMapping("/{userId}/password/reset")
    public Result<Void> resetPassword(@PathVariable("userId") Long[] userIds) {
        userService.resetPassword(userIds);
        return Result.success();
    }

    @InitBinder
    public void initBinder(final WebDataBinder webdataBinder) {
        webdataBinder.registerCustomEditor(User.UserState.class, new PropertyEditorSupport() {
            @Override
            public void setAsText(String text) throws IllegalArgumentException {
                setValue(User.UserState.valueOf(text));
            }
        });
    }
}
